﻿Imports System.Data.SqlClient
Public Class FormCategory
    Public Sub loaddata()
        Cmd.Parameters("@S") = "%" & txtSearch.Text & "%"
        Me.DataGridView1.DataSource = Cmd.ExecuteDataTable("select CategoryId,CategoryName,TypeName from Category C inner join [Type] T on T.TypeId = C.TypeId where upper(CategoryName) like upper(@S) and C.Active=1")
    End Sub
    Private Sub FormStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.DataGridView1)
        loaddata()
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        loaddata()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm = New FormCategoryEditor
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            loaddata()
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        If Me.DataGridView1.SelectedRows.Count = 0 Then Exit Sub

        Dim row = DataGridView1.SelectedRows(0)
        Dim frm = New FormCategoryEditor
        frm.txtId.Text = row.Cells("CategoryId").Value
        frm.txtName.Text = row.Cells("CategoryName").Value
        frm.cboName.SelectedValue = row.Cells("TypeName").Value
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            loaddata()
        End If
    End Sub

    Private Sub btnDelet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelet.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Dim row = DataGridView1.SelectedRows(0)
        Dim id = row.Cells("CategoryId").Value

        Cmd.Parameters("@id") = id
        Cmd.ExecuteNonQuery("UPDATE Category SET Active=0 WHERE CategoryId=@id")

        loaddata()
    End Sub
End Class
